//index.js
const app = getApp()
const db = wx.cloud.database()
const _ = db.command
const $ = _.aggregate
Page({
  data: {
    userInfo: {},
    today_consume: '0.00',
    month_consume: '0.00'
  },
  onLoad: function() {
    // db.collection('consume').aggregate()
    //   .skip(1)
    //   .end()
    //   .then(res => {
    //     console.log(res)
    //   }, err => {
    //     console.log(err)
    //   })
  },
  onShow: function () {
    this.initData()
  },
  // 云函数方法
  initData: function() {
    wx.showLoading({
      title: '正在加载..'
    })
    wx.cloud.callFunction({
      name: 'consume',
      data: {
        action: 'consume_index'
      },
      success: res => {
        wx.hideLoading()
        this.setData({
          today_consume: res.result.today_consume,
          month_consume: res.result.month_consume
        })
      },
      fail: err => {
        wx.hideLoading()
      }
    })
  },
  initData1: function() {
    //aggregate 方法
    const nowMonth = new Date().getMonth() + 1
    const nowDay = new Date().getDate()
    // 计算今日消费
    db.collection('consume').aggregate()
      .project({
        month: $.month('$create_time'),
        day: $.dayOfMonth('$create_time'),
        money: '$money'
      })
      .match({ month: nowMonth, day: nowDay })
      .group({
        _id: null,
        today_consume: $.sum('$money')
      })
      .end()
      .then(res => {
        wx.hideLoading()
        if (!res.list[0]) return
        let today_consume = res.list[0].today_consume
        this.setData({
          today_consume: today_consume.toFixed(2)
        })
      }, err => {
        console.log(err)
        wx.hideLoading()
      })
    // 计算本月消费
    db.collection('consume').aggregate()
      .project({
        month: $.month('$create_time'),
        money: '$money'
      })
      .match({ month: nowMonth })
      .group({
        _id: null,
        month_consume: $.sum('$money')
      })
      .end()
      .then(res => {
        wx.hideLoading()
        if (!res.list[0]) return
        let month_consume = res.list[0].month_consume
        this.setData({
          month_consume: month_consume.toFixed(2)
        })
      }, err => {
        console.log(err)
        wx.hideLoading()
      })
      
    return false

    // where 方法
    // 计算今日消费
    const today_start = new Date(new Date(new Date().toLocaleDateString()).getTime());
    const today_end = new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1);
    let today_consume = 0.00
    db.collection('consume').where({
      create_time: _.gte(today_start).and(_.lte(today_end))
    }).get({
      success: (res) => {
        res.data.map(item => {
          today_consume += parseFloat(item.money)
        })
        this.setData({
          today_consume: today_consume.toFixed(2)
        })
      }
    })
    // 计算今本月消费
    const month_start = new Date(new Date().getFullYear(), new Date().getMonth(), 1)  //本月开始时间
    var monthEndDate = new Date(new Date().getFullYear(), new Date().getMonth() + 1, 1);
    var days = (monthEndDate - month_start) / (1000 * 60 * 60 * 24)
    const month_end = new Date(new Date(new Date().getFullYear(), new Date().getMonth(), days).getTime() + 24 * 60 * 60 * 1000 - 1)  //本月结束时间
    let month_consume = 0.00
    db.collection('consume').where({
      create_time: _.gte(month_start).and(_.lte(month_end))
    }).get({
      success: (res) => {
        res.data.map(item => {
          month_consume += parseFloat(item.money)
        })
        this.setData({
          month_consume: month_consume.toFixed(2)
        })
      }
    })
  },
  go: function(e) {
    let url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url
    })
  }
})
